package com.pengzy.comment.web.thread;

import cn.hutool.core.collection.CollectionUtil;
import com.pengzy.comment.core.enums.ThreadKeyEnum;
import com.pengzy.comment.web.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;

/**
 * 全局线程池配置类
 * @author xiaozhi 2023/7/10
 */

@Component
@Slf4j
public class GlobalConfigThreadServer {

    @Resource
    private RedisCache redisCache;
    @Resource
    private Environment environment;

    /**
     * 创建日志线程池
     */
    @PostConstruct
    public void threadServer(){
        //logServer1();
    }



    public void logServer1(){
        Set<Object> cacheSet = redisCache.getCacheSet(ThreadKeyEnum.THREAD_GLOBAL_KEY.getKey());
        String property = environment.getProperty("server.name");
        String threadName=property+"-thread";
        if(CollectionUtil.isNotEmpty(cacheSet)&&cacheSet.contains(threadName)&&ThreadFactorys.getManagedExecutors(threadName)!=null){
            return;
        }
        log.info("创建模块线程池{}....",threadName);
        ExecutorService threadPoolServer = ThreadPoolFactory.getThreadPoolServer(ThreadKeyEnum.LOG.getKey(), 10);
        //注册线程池
        ThreadFactorys.register(threadName,threadPoolServer);
        log.info("注册{}线程池到线程池对象池",threadName);
        Map<String, ExecutorService> map = ThreadFactorys.getManagedExecutors();
        Set<String> strings = map.keySet();
        redisCache.setCacheSet(ThreadKeyEnum.THREAD_GLOBAL_KEY.getKey(),strings);
    }





}
